package com.sq.ai.service.impl;

import cn.hutool.system.UserInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sq.ai.exception.AiRunTimeException;
import com.sq.ai.mapper.SysConJobMapper;
import com.sq.ai.pojo.SysAlgor;
import com.sq.ai.pojo.SysConJob;
import com.sq.ai.service.SysAlgorService;
import com.sq.ai.mapper.SysAlgorMapper;
import com.sq.ai.service.SysConJobService;
import com.sq.ai.vo.SysAlgorVo;
import com.sq.common.enums.ReqTypeEnum;
import com.sq.common.exception.param.ParamNotComplete;
import com.sq.common.exception.param.RepeatDataException;
import com.sq.common.utils.ParamUtil;
import com.sq.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 *
 */
@Service
public class SysAlgorServiceImpl extends ServiceImpl<SysAlgorMapper, SysAlgor>
    implements SysAlgorService{

    @Autowired
    private SysAlgorMapper sysAlgorMapper;

    @Autowired
    private SysConJobMapper sysConJobMapper;

    @Override
    public boolean insertOrUpdate(SysAlgor sysAlgor) {

        boolean off = false;

        if (StringUtils.isNotEmpty(sysAlgor.getAlgorId())){
            // 更新
            sysAlgor.setUpdateTime(new Date());
            int i = sysAlgorMapper.updateById(sysAlgor);
            if (i > 0 ){
                off = true;
            }
        }else {
            ParamUtil.checkParam(sysAlgor, ReqTypeEnum.insert);

            QueryWrapper<SysAlgor> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("remark",sysAlgor.getRemark());

            SysAlgor one = getOne(queryWrapper);

            if (!Objects.isNull(one)){
                throw new RepeatDataException("当前调用码已被使用");
            }

            String idStr = IdWorker.getIdStr();
            sysAlgor.setAlgorId(idStr);
            sysAlgor.setCreateTime(new Date());
            off = save(sysAlgor);
        }

        return off;
    }

    @Override
    public List<SysAlgor> selectByWhere(SysAlgor sysAlgor) {
        return sysAlgorMapper.selectByWhere(sysAlgor);
    }

    @Override
    public boolean removeSysAlgor(String algorId) {

        // todo 判断当前算法是否进行了布控，如果布控了，报错
        SysConJob sysConJob = new SysConJob();
        sysConJob.setAlgorType(algorId);
        List<SysConJob> list = sysConJobMapper.selectConJob(sysConJob);

        if (list.size()>0){
            throw new AiRunTimeException("当前算法已被布控,请先撤除布控再删除！");
        }
        boolean b = removeById(algorId);
        return b;
    }


    @Override
    public SysAlgor getOneAlgor(String algorId) {
        if (StringUtils.isBlank(algorId)){
            throw new ParamNotComplete("算法id未传入");
        }
        QueryWrapper<SysAlgor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("algor_id",algorId);
        return getOne(queryWrapper);
    }

    @Override
    public List<SysAlgorVo> getAlgorLabel() {
        return sysAlgorMapper.getAlgorLabel();
    }
}




